using System;
using System.Text;

namespace Storage.Core
{
	internal class ErrorResponse : IStorageResponse
	{
		public ErrorResponse(string errorDescription)
		{
			ErrorDescription = errorDescription;
		}

		public byte[] Serialize()
		{
			var serializer = new FastBinarySerializer(sizeof (byte) + sizeof(Int32) + Encoding.UTF8.GetMaxByteCount(ErrorDescription.Length));
			serializer.Write(StorageOpCodes.Error);
			serializer.Write(ErrorDescription);
			return serializer.Buffer;
		}

		public static ErrorResponse Deserialize(byte[] content)
		{
			var deserializer = new FastBinaryDeserializer(content, 1);
			return new ErrorResponse(deserializer.ReadString());
		}

		public string ErrorDescription { get; private set; }
	}
}